#!/usr/local/bin/php
<?php
require_once 'common.php';

global $logger;
global $dbOAIICust;
global $dbOAIICode;
global $codeModel;

$sql = "SELECT t.SERVICE_ID,t.STATUS,t.SERVICE_NAME,t.SERVICE_CODE,t.SP_ID,t.NOTES,
	TO_CHAR(t.UPDATETIME, 'YYYY-MM-DD HH24:MI:SS') AS UPDATETIME, t2.SP_CODE
	FROM TBCODESERVICE t LEFT JOIN TBCODESP t2 ON (t.SP_ID=t2.SP_ID) ORDER BY t.SERVICE_CODE";
$st = oci_parse($dbOAIICust, $sql);
if (!oci_execute($st)) {
    $e = oci_error($st);
    $logger->err("oci_execute failed: code-{$e['code']} message-{$e['message']} sql-{$e['sqltext']}");
    return false;
}
$services = array();
oci_fetch_all($st, $services, null, null, OCI_FETCHSTATEMENT_BY_ROW + OCI_ASSOC);
foreach ($services as $service) {
    $companyId = (int) $service['SERVICE_CODE'];   
    $app = $codeModel->getApp($companyId, OaAppModel::APP_CONFIG_ENV_PRODUCTION);
    if ($app && $app['SERVICE_NAME'] != $service['SERVICE_NAME']) {
        $logger->err("app name conflicted: app-$companyId code_name-{$app['SERVICE_NAME']} cust_name-{$service['SERVICE_NAME']}");
    }
    $app = $codeModel->getAppByName($service['SERVICE_NAME'], OaAppModel::APP_CONFIG_ENV_PRODUCTION);
    if ($app && $app['SERVICE_ID'] != $service['SERVICE_CODE']) {
        $logger->err("app id conflicted: app-{$service['SERVICE_NAME']} code_id-{$app['SERVICE_ID']} cust_id-{$service['SERVICE_CODE']}");
    }
}
